package com.motu.monstercity.server.game.logic;

import com.motu.monstercity.server.game.commondata.bag.Box;
import com.motu.monstercity.server.game.commondata.bag.BoxItem;
import com.motu.monstercity.server.game.commondata.bag.Item;
import com.motu.monstercity.server.game.commondata.bag.ReviewItem;
import com.motu.monstercity.server.game.commondata.bag.ChipMain;
import com.motu.vertx.module.utility.mysql.MysqlClient;
import com.motu.vertx.module.utility.mysql.MysqlTool;
import com.motu.vertx.module.utility.toolset.Tool;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.json.JsonArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class BagBaseData {
    private static Logger logger = LoggerFactory.getLogger(BagBaseData.class);
    private static ConcurrentHashMap<Integer, Item> itemMap = new ConcurrentHashMap<>(); // 道具表
    private static ConcurrentHashMap<Integer, Box> boxMap = new ConcurrentHashMap<>(); // 宝箱表
    private static ConcurrentHashMap<Integer, BoxItem> boxItemMap = new ConcurrentHashMap<>(); // 宝箱物品表
    private static ConcurrentHashMap<Integer, ReviewItem> reviewItemMap = new ConcurrentHashMap<>(); // 评审服测试服后台发放道具表

    private static ConcurrentHashMap<Integer, ChipMain> chipMainMap = new ConcurrentHashMap<>(); // 碎片配置表


    // 公共数据初始化
    public static void init(List<Future> futures) {
        futures.add(initItem());
        futures.add(initBox());
        futures.add(initBoxItem());
        futures.add(initReviewItem());
        futures.add(initChipMain());
    }

    /**
     * 初始化道具表
     */
    public static Future<Void> initItem() {
        String table = "item";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, Item> tmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        Item entity = new Item(result.getJsonObject(i));
                        tmp.put(entity.getId(), entity);
                    }
                    itemMap = tmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static Item getItem(int id) {
        Item result = itemMap.get(id);
        if (result == null) {
            logger.error("getItem error key = " + id);
        }
        return result;
    }
    public static ConcurrentHashMap<Integer, Item> getItemMap() { return itemMap; }

    /**
     * 初始化宝箱表
     */
    public static Future<Void> initBox() {
        String table = "box";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, Box> tmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        Box entity = new Box(result.getJsonObject(i));
                        tmp.put(entity.getBoxId(), entity);
                    }
                    boxMap = tmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static Box getBox(int boxId) {
        Box result = boxMap.get(boxId);
        if (result == null) {
            logger.error("getBox error key = " + boxId);
        }
        return result;
    }
    public static ConcurrentHashMap<Integer, Box> getBoxMap() { return boxMap; }

    /**
     * 初始化宝箱物品表
     */
    public static Future<Void> initBoxItem() {
        String table = "box_item";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, BoxItem> tmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        BoxItem entity = new BoxItem(result.getJsonObject(i));
                        tmp.put(entity.getId(), entity);
                    }
                    boxItemMap = tmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static BoxItem getBoxItem(int id) {
        BoxItem result = boxItemMap.get(id);
        if (result == null) {
            logger.error("getBoxItem error key = " + id);
        }
        return result;
    }
    public static ConcurrentHashMap<Integer, BoxItem> getBoxItemMap() { return boxItemMap; }

    public static List<BoxItem> getBoxItemList(int boxId) {
        List<BoxItem> result = new ArrayList<>();
        for (BoxItem boxItem : getBoxItemMap().values()) {
            if (boxItem.getBoxId() == boxId) {
                result.add(boxItem);
            }
        }
        return result;
    }

    /**
     * 初始化评审服测试服后台发放道具表
     */
    public static Future<Void> initReviewItem() {
        String table = "review_item";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getServerPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, ReviewItem> tmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        ReviewItem entity = new ReviewItem(result.getJsonObject(i));
                        tmp.put(entity.getItemId(), entity);
                    }
                    reviewItemMap = tmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static ReviewItem getReviewItem(int id) {
        ReviewItem result = reviewItemMap.get(id);
        if (result == null) {
            logger.error("getReviewItem error key = " + id);
        }
        return result;
    }
    public static ConcurrentHashMap<Integer, ReviewItem> getReviewItemMap() { return reviewItemMap; }

    /**
     * 初始化碎片配置表
     */
    public static Future<Void> initChipMain() {
        String table = "chip_main";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, ChipMain> tmp = new ConcurrentHashMap<>();
                    ConcurrentHashMap<Integer, List<ChipMain>> tmpTypeMap = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        ChipMain entity = new ChipMain(result.getJsonObject(i));
                        tmp.put(entity.getChipId(), entity);
                        tmpTypeMap.computeIfAbsent(entity.getType(), k -> new ArrayList<>()).add(entity);
                    }
                    chipMainMap = tmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static ChipMain getChipMain(int id) {
        ChipMain result = chipMainMap.get(id);
        if (result == null) {
            logger.error("getChipMain error key = " + id);
        }
        return result;
    }
    public static ConcurrentHashMap<Integer, ChipMain> getChipMainMap() { return chipMainMap; }
}
